package rpa

import (
	"context"
	"git.mycaigou.com/gfyx/common/tenant_db"
)

/**
 * @Author: xiuliu
 * @Description:
 * @File:  rpa_repository_iface
 * @Version: 1.0.0
 * @Date: 2023/10/30 16:46
 */

type RpaRepository struct {
	tenant_db.TenantDB
}

func NewRpaRepository() RpaRepositoryIface {
	return &RpaRepository{}
}

func (r *RpaRepository) InsertData(ctx context.Context, data *RpaConfig) (bool, error) {
	err := r.GetTenantDB(ctx).Create(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (r *RpaRepository) UpdateData(ctx context.Context, data *RpaConfig) (bool, error) {
	err := r.GetTenantDB(ctx).Save(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (r *RpaRepository) GetInfo(ctx context.Context, condition map[string]interface{}) (*RpaConfig, error) {
	rpaConfig := RpaConfig{}
	err := r.GetTenantDB(ctx).Where(condition).First(&rpaConfig).Error
	if err != nil {
		return nil, err
	}
	return &rpaConfig, nil
}

// 保存RPA启动日志
func (r *RpaRepository) InsertStartLog(ctx context.Context, data *RpaStartLog) (bool, error) {
	err := r.GetTenantDB(ctx).Create(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

// 校验RPA token
func (r *RpaRepository) CheckRpaToken(ctx context.Context, condition map[string]interface{}) (*RpaStartLog, error) {
	log := RpaStartLog{}
	err := r.GetTenantDB(ctx).Where(condition).Order("login_time DESC").First(&log).Error
	if err != nil {
		return nil, err
	}
	return &log, nil
}
